<?php

require '../claroline/inc/claro_init_global.inc.php';

if (!get_init('is_authenticated') ) claro_disp_auth_form(true);

include_once('lib/conf.php');
include_once('lib/portfolioNumerique.lib.php');

// Classes pour la gestion des documents
include_once('lib/classes/document.class.php');
include_once('lib/classes/documentCompetence.class.php');


if (!$_REQUEST['documentId'] && !$_REQUEST['zip'])
exit;

if (!$_REQUEST['userId'])
$userId = claro_get_current_user_id();


// En cas de zip, on découpe la liste des documents
if ($_REQUEST['zip'] && $_REQUEST['listeDoc'])
{
	$nom_fichier = "monPortfolioNumerique.zip";

	$filename = $cheminSystemeDocument.$userId."/".$nom_fichier;

	$zip = new ZipArchive();

	if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
	    exit("cannot open <$filename>\n");
	}


	$listeDocs = explode("_",$_REQUEST['listeDoc']);

	foreach ($listeDocs as $documentId)
	{
		$document = new PFLdocument();
		$document->load($documentId);

		$zip->addFile($cheminSystemeDocument.$document->getUserId()."/".$document->getChemin(),trim($document->getFichier()));
	}

	$zip->close();

	header("Cache-Control: public");
	header("Pragma:");
	header("Expires: 0");
	header("Content-Type: application/force-download" );
	header("Content-Disposition: attachment; filename=\"".$nom_fichier."\";");
	header("Content-Description: File Transfert");
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: " .filesize( $filename ) );
	readfile($filename);

	unlink($filename);
}
else
{
	$document = new PFLdocument();
	$document->load($_REQUEST['documentId']);

	$filename = $cheminSystemeDocument.$document->getUserId()."/".$document->getChemin();

	header('Content-Description: File Transfer');
	header('Content-type: application/octet-stream');
	header('Content-Disposition: attachment; filename="'.$document->getFichier().'"');

	$handle = fopen ($filename, "r");
	$contents = fread ($handle, filesize ($filename));
	print $contents;
	fclose ($handle);

	exit;
}
?>
